/**
 * Notes: API服务层
 * Ver : WeCollege Server API 1.0.0
 * Date: 2025-01-27
 * Description: 封装具体的业务API调用，提供类型安全的接口
 */

const apiHelper = require('./api_helper.js');
const { getEndpoint } = require('./api_endpoints.js');
const { validatePageQuery } = require('./api_types.js');

// ==================== 用户认证服务 ====================
const AuthService = {
  /**
   * 微信登录
   * @param {string} code - 微信登录code
   * @returns {Promise<UserLoginVO>}
   */
  async login(code) {
    const endpoint = getEndpoint('AUTH', 'LOGIN');
    const result = await apiHelper.post(endpoint, { code });
    return result.data;
  }
};

// ==================== 帖子服务 ====================
const PostService = {
  /**
   * 发布帖子
   * @param {PostDto} postData - 帖子数据
   * @returns {Promise<ApiResponse>}
   */
  async insert(postData) {
    const endpoint = getEndpoint('POST', 'INSERT');
    return await apiHelper.post(endpoint, postData, { title: '发布中...' });
  },

  /**
   * 更新帖子
   * @param {Post} postData - 帖子数据
   * @returns {Promise<ApiResponse>}
   */
  async update(postData) {
    const endpoint = getEndpoint('POST', 'UPDATE');
    return await apiHelper.post(endpoint, postData, { title: '更新中...' });
  },

  /**
   * 删除帖子
   * @param {string} postId - 帖子ID
   * @returns {Promise<ApiResponse>}
   */
  async delete(postId) {
    const endpoint = getEndpoint('POST', 'DELETE', { postId });
    return await apiHelper.delete(endpoint, { title: '删除中...' });
  },

  /**
   * 查询主页展示帖子
   * @param {PostQuery} query - 查询条件
   * @returns {Promise<PostVo[]>}
   */
  async query(query) {
    const endpoint = getEndpoint('POST', 'QUERY');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  },

  /**
   * 分页查询我的帖子
   * @param {PageQuery} query - 查询条件
   * @returns {Promise<PageResponse<Post>>}
   */
  async queryMyPost(query) {
    const endpoint = getEndpoint('POST', 'QUERY_MY');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.get(endpoint, { 
      params: validQuery,
      title: 'bar' 
    });
    return result.data;
  },

  /**
   * 获取单个帖子详情
   * @param {string} postId - 帖子ID
   * @returns {Promise<Post>}
   */
  async getSingle(postId) {
    const endpoint = getEndpoint('POST', 'GET_SINGLE', { postId });
    const result = await apiHelper.get(endpoint, { title: 'bar' });
    return result.data;
  },

  /**
   * 点赞帖子
   * @param {string} postId - 帖子ID
   * @returns {Promise<ApiResponse>}
   */
  async like(postId) {
    const endpoint = getEndpoint('POST', 'LIKE', { postId });
    return await apiHelper.post(endpoint, {}, { showLoading: false });
  },

  /**
   * 取消点赞
   * @param {string} postId - 帖子ID
   * @returns {Promise<ApiResponse>}
   */
  async dislike(postId) {
    const endpoint = getEndpoint('POST', 'DISLIKE', { postId });
    return await apiHelper.post(endpoint, {}, { showLoading: false });
  },

  /**
   * 访问帖子
   * @param {string} postId - 帖子ID
   * @returns {Promise<ApiResponse>}
   */
  async visit(postId) {
    const endpoint = getEndpoint('POST', 'VISIT', { postId });
    return await apiHelper.post(endpoint, {}, { showLoading: false });
  }
};

// ==================== 评论服务 ====================
const CommentService = {
  /**
   * 发表评论
   * @param {CommentDto} commentData - 评论数据
   * @returns {Promise<ApiResponse>}
   */
  async insert(commentData) {
    const endpoint = getEndpoint('COMMENT', 'INSERT');
    return await apiHelper.post(endpoint, commentData, { title: '评论中...' });
  },

  /**
   * 删除评论
   * @param {string} commentId - 评论ID
   * @returns {Promise<ApiResponse>}
   */
  async delete(commentId) {
    const endpoint = getEndpoint('COMMENT', 'DELETE', { commentId });
    return await apiHelper.post(endpoint, {}, { title: '删除中...' });
  },

  /**
   * 点赞评论
   * @param {string} commentId - 评论ID
   * @returns {Promise<ApiResponse>}
   */
  async like(commentId) {
    const endpoint = getEndpoint('COMMENT', 'LIKE', { commentId });
    return await apiHelper.post(endpoint, {}, { showLoading: false });
  },

  /**
   * 取消点赞评论
   * @param {string} commentId - 评论ID
   * @returns {Promise<ApiResponse>}
   */
  async dislike(commentId) {
    const endpoint = getEndpoint('COMMENT', 'DISLIKE', { commentId });
    return await apiHelper.post(endpoint, {}, { showLoading: false });
  },

  /**
   * 根据帖子查询需要展示的评论
   * @param {CommentQuery} query - 查询条件
   * @returns {Promise<PageResponse<CommentVo>>}
   */
  async queryByPost(query) {
    const endpoint = getEndpoint('COMMENT', 'QUERY_BY_POST');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  },

  /**
   * 查询更多子评论
   * @param {MoreSubCommentPageQuery} query - 查询条件
   * @returns {Promise<SubCommentVo[]>}
   */
  async queryMoreSub(query) {
    const endpoint = getEndpoint('COMMENT', 'QUERY_MORE_SUB');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  }
};

// ==================== 失物招领服务 ====================
const LostService = {
  /**
   * 上传失物
   * @param {LostDto} lostData - 失物数据
   * @returns {Promise<ApiResponse>}
   */
  async insert(lostData) {
    const endpoint = getEndpoint('LOST', 'INSERT');
    return await apiHelper.post(endpoint, lostData, { title: '发布中...' });
  },

  /**
   * 编辑失物描述
   * @param {LostUpdateDto} lostData - 失物数据
   * @returns {Promise<ApiResponse>}
   */
  async update(lostData) {
    const endpoint = getEndpoint('LOST', 'UPDATE');
    return await apiHelper.post(endpoint, lostData, { title: '更新中...' });
  },

  /**
   * 删除我的失物
   * @param {string} lostId - 失物ID
   * @returns {Promise<ApiResponse>}
   */
  async delete(lostId) {
    const endpoint = getEndpoint('LOST', 'DELETE', { lostId });
    return await apiHelper.delete(endpoint, { title: '删除中...' });
  },

  /**
   * 首页展示失物
   * @param {LostQuery} query - 查询条件
   * @returns {Promise<PageResponse<Lost>>}
   */
  async list(query) {
    const endpoint = getEndpoint('LOST', 'LIST');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  },

  /**
   * 分页查询我的失物
   * @param {PageQuery} query - 查询条件
   * @returns {Promise<PageResponse<Lost>>}
   */
  async queryMy(query) {
    const endpoint = getEndpoint('LOST', 'QUERY_MY');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, validQuery);
    return result.data;
  },

  /**
   * 获取单个失物详情
   * @param {string} lostId - 失物ID
   * @returns {Promise<Lost>}
   */
  async getOne(lostId) {
    const endpoint = getEndpoint('LOST', 'GET_ONE', { lostId });
    const result = await apiHelper.get(endpoint, { title: 'bar' });
    return result.data;
  }
};

// ==================== 交易服务 ====================
const TradeService = {
  /**
   * 新增商品
   * @param {TradeDto} tradeData - 商品数据
   * @returns {Promise<ApiResponse>}
   */
  async add(tradeData) {
    const endpoint = getEndpoint('TRADE', 'ADD');
    return await apiHelper.post(endpoint, tradeData, { title: '发布中...' });
  },

  /**
   * 修改商品
   * @param {TradeUpdateDto} tradeData - 商品数据
   * @returns {Promise<ApiResponse>}
   */
  async update(tradeData) {
    const endpoint = getEndpoint('TRADE', 'UPDATE');
    return await apiHelper.post(endpoint, tradeData, { title: '更新中...' });
  },

  /**
   * 下架我的商品
   * @param {string} tradeId - 商品ID
   * @returns {Promise<ApiResponse>}
   */
  async delete(tradeId) {
    const endpoint = getEndpoint('TRADE', 'DELETE', { tradeId });
    return await apiHelper.delete(endpoint, { title: '删除中...' });
  },

  /**
   * 单独查询商品
   * @param {string} tradeId - 商品ID
   * @returns {Promise<Trade>}
   */
  async query(tradeId) {
    const endpoint = getEndpoint('TRADE', 'QUERY', { tradeId });
    const result = await apiHelper.get(endpoint, { title: 'bar' });
    return result.data;
  },

  /**
   * 查询主页展示商品
   * @param {MainTradeQuery} query - 查询条件
   * @returns {Promise<PageResponse<Trade>>}
   */
  async queryMain(query) {
    const endpoint = getEndpoint('TRADE', 'QUERY_MAIN');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  },

  /**
   * 分页查询我的商品
   * @param {TradeQuery} query - 查询条件
   * @returns {Promise<PageResponse<Trade>>}
   */
  async queryMy(query) {
    const endpoint = getEndpoint('TRADE', 'QUERY_MY');
    const validQuery = validatePageQuery(query);
    const result = await apiHelper.post(endpoint, { ...validQuery, ...query });
    return result.data;
  }
};

// ==================== 通用服务 ====================
const CommonService = {
  /**
   * 查询所有帖子分类
   * @returns {Promise<Type[]>}
   */
  async queryType() {
    const endpoint = getEndpoint('COMMON', 'QUERY_TYPE');
    const result = await apiHelper.get(endpoint, { title: 'bar' });
    return result.data;
  },

  /**
   * 查询轮播图
   * @returns {Promise<PageResponse<BackgroundVo>>}
   */
  async getBackground() {
    const endpoint = getEndpoint('COMMON', 'GET_BACKGROUND');
    const result = await apiHelper.get(endpoint, { title: 'bar' });
    return result.data;
  }
};

// ==================== 上传服务 ====================
const UploadService = {
  /**
   * 上传图片
   * @param {File} file - 图片文件
   * @returns {Promise<ApiResponse>}
   */
  async image(file) {
    const endpoint = getEndpoint('UPLOAD', 'IMAGE');
    return await apiHelper.post(endpoint, { file }, { title: '上传中...' });
  }
};

// 导出所有服务
module.exports = {
  AuthService,
  PostService,
  CommentService,
  LostService,
  TradeService,
  CommonService,
  UploadService,
};